package com.hqyj.ssm_project.controller;

import com.hqyj.ssm_project.exception.ControllerException;
import com.hqyj.ssm_project.pojo.*;
import com.hqyj.ssm_project.service.AttendanceService;
import com.hqyj.ssm_project.service.UserService;
import com.hqyj.ssm_project.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AttendanceController {
    @Autowired
    private AttendanceService attendanceService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/swringCard.do", method = RequestMethod.POST)
    public @ResponseBody
    ResfulCard swingCard(@RequestBody ResfulCard resfulCard) throws ParseException {
        String cardNum = resfulCard.getCard_num();
        int length = cardNum.length();
        if (cardNum.length() < 16) {
            for (int i = 0; i < 16 - length; i++) {
                cardNum = "0" + cardNum;
            }
        }
        resfulCard.setCard_status(0);
        User user = userService.findCardUser(cardNum);
        if (user != null) {
            if ("Y".equals(user.getUserStatus())) {
                resfulCard.setCard_num(cardNum);
                resfulCard.setCard_status(1);
                Date date = new Date();
                attendanceService.insertDateAndTime(date, user.getUserId());
            } else if ("N".equals(user.getUserStatus())) {
                resfulCard.setCard_num(cardNum);
                resfulCard.setCard_status(0);
            }else{
                resfulCard.setCard_num(cardNum);
                resfulCard.setCard_status(0);
            }
        }
        System.out.println(cardNum + "长度：" + cardNum.length());

        return resfulCard;
    }

    //查询某天所有人的考勤
    @RequestMapping(value = "/attendancPage.do")
    public String attendancPage(String date, Model model) {
        if (date == null) {
            SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            date = sDateFormat.format(new Date());
        }
        List<AttendanceDate> dateList = attendanceService.selectAllUserAttendanceByDay(date);
        model.addAttribute("dateList", dateList);
        return "attendancPage";
    }

    @RequestMapping(value = "/attendancPagebyDay.do", method = RequestMethod.POST)
    public @ResponseBody
    List<AttendanceDate> attendancPagebyDay(String date) {
        System.out.println("------------" + date);
        List<AttendanceDate> dateList = attendanceService.selectAllUserAttendanceByDay(date);
        return dateList;
    }

    //用户列表
    @RequestMapping("/userToAttendancPage.do")
    public String userPage(Integer currentNum, Model model) throws ControllerException {
        if (currentNum == null) {
            currentNum = 1;
        }
        PageUtil<User> userPageUtil = userService.findUserByPageNumber(currentNum);
        model.addAttribute("userlist", userPageUtil);

        return "userToattendancPage";
    }

    //查询某人一个月的考勤
    @RequestMapping(value = "/findUserAttendanceByUserId.do")
    public String findUserAttendanceByUserId(Integer userId, String date, Model model) {
        System.out.println(userId + "---" + date);
        if (date == null) {
            SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            date = sDateFormat.format(new Date());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("date", date);
        map.put("userId", userId);
        List<AttendanceDate> list = attendanceService.findUserAttendanceByUserId(map);
        model.addAttribute("attendancelist", list);
        model.addAttribute("date",date);
        return "findUserAttendanceByUserIdPage";
    }
    @PostMapping("/echartToAttendance.do")
    public @ResponseBody AttendanceStatis echartToAttendance(@RequestBody Map<String,Object> map){
        String userIdstr =  (String) map.get("userId");
        int userId = Integer.parseInt(userIdstr);
        String date = (String) map.get("date");
        if(date==null){
            SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            date = sDateFormat.format(new Date());
        }
        Map<String,Object> mapToMapper=new HashMap<>();
        mapToMapper.put("userId",userId);
        mapToMapper.put("date",date);
        AttendanceStatis attendanceStatis=attendanceService.selectUserAttendanceStatis(mapToMapper);
       attendanceStatis.setUserId(userId);

        return attendanceStatis;
    }
}
